package com.caojing.haolemai.ui.outlets;


import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.caojing.haolemai.R;
import com.caojing.haolemai.base.BaseFragment;
import com.caojing.haolemai.bean.TabResultsEntity;
import com.caojing.haolemai.config.UrlConfig;
import com.caojing.haolemai.ui.outlets.outletsSharefragment.CustomPopupWindow;
import com.caojing.haolemai.ui.outlets.outletsSharefragment.OutletsShareFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;


/**
 * A simple {@link Fragment} subclass.
 */
public class OutletsFragment extends BaseFragment implements OutletsContract.View {

    @Bind(R.id.iv_outlets_logo)
    ImageView ivOutletsLogo;
    @Bind(R.id.iv_outlets_share)
    ImageView ivOutletsShare;
    @Bind(R.id.tablayout_outlets)
    TabLayout tablayoutOutlets;
    @Bind(R.id.viewpager_outlets)
    ViewPager viewpagerOutlets;

    private OutletsContract.Presenter presenter;


    private List<TabResultsEntity> Tablist;
    private TabResultsEntity tabResultsEntity;
    //用于区分每个fragment的不同之处
    private int[] CatidList;
    private List<OutletsShareFragment> outletsShareFragmentList = new ArrayList<>();
    private OutletsFragmentViewpagerAdapter adapter;

    //自定义的弹出框类
    CustomPopupWindow menuWindow;



    @Override
    protected void initData() {
        Tablist = new ArrayList<>();
        presenter = new OutletsPresenter(this);
        presenter.getVertical(initParams());


        //设置指示器的颜色
        tablayoutOutlets.setSelectedTabIndicatorColor(Color.RED);
        //设置tab的文本颜色
        tablayoutOutlets.setTabTextColors(Color.BLACK, Color.RED);

        //设置mode 滚动模式动态改变tabLayout宽度
        tablayoutOutlets.setTabMode(TabLayout.MODE_SCROLLABLE);
        tablayoutOutlets.getTabGravity();




    }

    @Override
    protected void initView(View view) {
        ShareSDK.initSDK(getContext());//初始化sharedsdk





    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_outlets;
    }

    public Map<String, String> initParams() {

        Map<String, String> params = new HashMap<>();
        params.put(UrlConfig.OutletsKey.SESSION_ID, UrlConfig.OutletsValue.SESSION_ID);
        params.put(UrlConfig.OutletsKey.APP_ID, UrlConfig.OutletsValue.APP_ID);
        params.put(UrlConfig.OutletsKey.SITE_MASK, UrlConfig.OutletsValue.SITE_MASK);
        params.put(UrlConfig.OutletsKey.OS, UrlConfig.OutletsValue.OS);
        params.put(UrlConfig.OutletsKey.VERSION, UrlConfig.OutletsValue.VERSION);
        return params;
    }


    @OnClick(R.id.iv_outlets_share)
    public void onClick() {
    // todo:这里点击是一个菜单
        //实例化SelectPicPopupWindow
//        menuWindow = new CustomPopupWindow(getActivity(), itemsOnClick);
//
//        //显示窗口
//        if (menuWindow!=null){
//            menuWindow.showAtLocation(getActivity().findViewById(R.id.pop_layout),Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
//            //设置layout在PopupWindow中显示的位置
//        }

        //1.创建一键分享的对象
        OnekeyShare onekeyShare=new OnekeyShare();
        onekeyShare.disableSSOWhenAuthorize();//关闭sso授权
        //设置一键分享的内容
        onekeyShare.setTitle("一键分享的标题");
        onekeyShare.setText("一键分享的内容");
//        onekeyShare.setImageUrl("http://img4.imgtn.bdimg.com/it/u=2291132731,375716043&fm=21&gp=0.jpg");
        onekeyShare.show(getContext());




    }
    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener(){

        public void onClick(View v) {
            menuWindow.dismiss();
            switch (v.getId()) {
                case R.id.textview_wx_friend:
                    Toast.makeText(getContext(),"菜单",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.textview_wx_circle:
                    Toast.makeText(getContext(),"菜单",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }


        }

    };




    @Override
    public void onGetVerticalSucess(List<TabResultsEntity> tabResultsEntities) {
        Tablist.addAll(tabResultsEntities);
        //Log.i("tag---------------", "" + Tablist.size());   //这个数据可以得到
//        initData();

        for (int i = 0; i < Tablist.size(); i++) {
            //Log.i("tag---------------", "" + Tablist.get(i).getName());   //这个数据可以得到
            tabResultsEntity = Tablist.get(i);
//            得到需要区分每个fragment的id值
            OutletsShareFragment outletsShareFragment = OutletsShareFragment.newInstance(tabResultsEntity.getCatid() + "");
            Log.i("TTAA",tabResultsEntity.getCatid() + "123");
            outletsShareFragmentList.add(outletsShareFragment);
        }


        adapter = new OutletsFragmentViewpagerAdapter(getChildFragmentManager(), outletsShareFragmentList, Tablist);
        viewpagerOutlets.setAdapter(adapter);

        //将tablayout和viewpager进行关联
        tablayoutOutlets.setupWithViewPager(viewpagerOutlets);

    }

    @Override
    public void onGetVerticalFail(String erro) {
        Log.i("tag", erro);
    }






    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

}
